package cn.hn.tx.rebot.task.job;

import java.util.Iterator;
import java.util.List;

import cn.hn.comm.Tools;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.service.ICrawlBlogService;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;
import cn.hn.tx.rebot.task.model.TaskExeLog;

/**
 * 抓取日志任务
 * 
 * @author Administrator
 * 
 */
public class TaskJob_CrawlBlog extends ITaskJob {

	// 抓取开始页
	private int crawlStartPage;
	private int crawlEndPage;
	// url前缀
	private String url_blogListPrefix = "http://club.eladies.sina.com.cn/forum-25-";
	// url后缀
	private String url_blogListPostfix = ".html";
	// 抓取任务类型
	private int crawl_type;
	final public static int CRAWL_TYPE_SINA_SinaEladies = 0;// 新浪女性

	// 抓取服务
	private ICrawlBlogService crawlService;
	// 机器日志实现
	private IRebotBlogService rebotBlogService;
	
	private TaskType taskType;

	@Override
	void executeJob() {
		System.out.println("开始:"+getTaskName());
		// 任务执行记录日志
		TaskExeLog taskExeLog = new TaskExeLog();
		taskExeLog.setStartTime(Tools.getCurDate(null));
		taskExeLog.setTaskName(this.getTaskName());
		taskExeLog.setType(type().getType());
		String taskLogMemo;

		try {
			int iBlogCount=0;
			String url;
			// 列表主页抓取
			for (int i = crawlStartPage; i <= crawlEndPage; i++) {
				// 列表地址
				url = url_blogListPrefix + i + url_blogListPostfix;
				// 获取列表中的日志
				List<Blog> listBlogs = crawlService.getBlogs(url);
				if (listBlogs != null) {
					// 迭代日志
					Iterator<Blog> iterBlog = listBlogs.iterator();
					while (iterBlog.hasNext()) {
						Blog blog = iterBlog.next();
						// 日志写入
						rebotBlogService.insertRebotWriteBlog_HandleHref("小三",
								"http://www.tianxin007.com", blog.getTitle(),
								blog.getContent(), blog.getUrl(), crawl_type,
								Tools.getCurDate(null), null, 0);
						iBlogCount++;
					}
				}
			}
			taskLogMemo="抓取日志:"+iBlogCount;
		} catch (Exception e) {
			//记录异常
			getExceptionService().insertException(this.getClass().getName(), "executeJob()", e.toString());
			taskLogMemo="任务执行失败.原因:"+e.toString();
		}
		taskExeLog.setMemo(taskLogMemo);
		taskExeLog.setEndTime(Tools.getCurDate(null));
		//记录任务执行日志
		getTaskService().insertTaskExeLog(taskExeLog);
		System.out.println("结束:"+getTaskName());
	}

	@Override
	public TaskType type() {
		return taskType;
	}

	public void setCrawlStartPage(int crawlStartPage) {
		this.crawlStartPage = crawlStartPage;
	}

	public void setCrawlEndPage(int crawlEndPage) {
		this.crawlEndPage = crawlEndPage;
	}

	public void setUrl_blogListPrefix(String url_blogListPrefix) {
		url_blogListPrefix = url_blogListPrefix;
	}

	public ICrawlBlogService getCrawlService() {
		return crawlService;
	}

	public void setCrawlService(ICrawlBlogService crawlService) {
		this.crawlService = crawlService;
	}

	public IRebotBlogService getRebotBlogService() {
		return rebotBlogService;
	}

	public void setRebotBlogService(IRebotBlogService rebotBlogService) {
		this.rebotBlogService = rebotBlogService;
	}

	public int getCrawlStartPage() {
		return crawlStartPage;
	}

	public int getCrawlEndPage() {
		return crawlEndPage;
	}

	public String getUrl_blogListPrefix() {
		return url_blogListPrefix;
	}

	public int getCrawl_type() {
		return crawl_type;
	}

	public void setCrawl_type(int crawl_type) {
		this.crawl_type = crawl_type;
	}

	public String getUrl_blogListPostfix() {
		return url_blogListPostfix;
	}

	public void setUrl_blogListPostfix(String url_blogListPostfix) {
		this.url_blogListPostfix = url_blogListPostfix;
	}

	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

}
